const APIUrl = require('../../../../tools/APIurl');
const mAPIUrl = new APIUrl()
const date = new Date()
var that
//时间格式
const formatNumber = (n) => {
  n = n.toString();
  return n[1] ? n : '0' + n;
};
const formatTime = (date) => {
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hour = date.getHours();
  const minute = date.getMinutes();
  const second = date.getSeconds();
  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':');
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    array:['壹城中心九区','壹城中心八区','壹城中心七区','壹城中心六区'],
    address:[{
      address:'壹城中心九区',
      houseNamber:'8栋1单元208',
    }],
    info:[{
      title:'姓名',
      content:'请输入租客姓名',
      events:'bindName'
    },{
      title:'电话',
      content:'请输入租客电话',
      events:'bindNumber'
    },{
      title:'身份证',
      content:'请输入租客身份证号',
      events:'bindIdentify'
    },
  ],
  changeDate:[{
    title:'起租日期',
    content:'请选择',
    events:'bindDateChange'
  },{
    title:'到租日期',
    content:'请选择',
    events:'bindDateChange'
  },
  ],
  addressId:1,
  roomId:0,
  onDate:formatTime(date),
  userInfo:[{
    name:'',
    phone_number:'',
    identify_number:'',
    identify_positive:'',
    identify_reverse:''
  }],
  rent:'',
  },
  bindDateChange: function(e) {
    var index= e.currentTarget.dataset.index
    // console.log('picker发送选择改变，携带值为', index)
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    var changeDate = this.data.changeDate
    changeDate[index].content=e.detail.value
    this.setData({
      changeDate: changeDate
    })
  },
  setAmount(){
   wx.navigateTo({
     url: './setAmount/setAmount',
   })
  },
  addTenant(){
    wx.navigateTo({
      url: './addTenant/addTenant',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this
    console.log(options)
    var address = [{
      address:options.address,
      houseNamber:options.number,
    }]
    that.setData({
      address:address,
      addressId:options.address_id,
      roomId:options.id
    })
  },
  // form内容
  formSubmit: function(e) {
    console.info('表单提交携带数据', e.detail.value)
  },
  // 名字
  bindName(e){
    var userInfo = that.data.userInfo
    userInfo[0].name=e.detail.value
    that.setData({
      userInfo:userInfo
    })
  },
  bindNumber(e){
    var userInfo = that.data.userInfo
    userInfo[0].phone_number=e.detail.value
    that.setData({
      userInfo:userInfo
    })
  },
  bindIdentify(e){
    var userInfo = that.data.userInfo
    userInfo[0].identify_number=e.detail.value
    that.setData({
      userInfo:userInfo
    })
  },
  bindRent(e){
    var rent = e.detail.value
    that.setData({
      rent:rent
    })
  },
  identify_positive(e){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        var userInfo = that.data.userInfo
        userInfo[0].identify_positive=tempFilePaths
        that.setData({
          userInfo:userInfo
        })
        wx.showToast({
          title: '上传成功',
        })
      }
    })
  },
  identify_reverse(e){
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        console.log(tempFilePaths)
        var userInfo = that.data.userInfo
        userInfo[0].identify_reverse=tempFilePaths
        that.setData({
          userInfo:userInfo
        })
        wx.showToast({
          title: '上传成功',
        })
      }
    })
  },
  save(){
    mAPIUrl.save('/api/mgr/customers',{
      action: 'add_customer',
      data:{
        name: that.data.userInfo[0].name,
        gender:0,
        phone_number:that.data.userInfo[0].phone_number,
        identify_number:that.data.userInfo[0].identify_number,
        identify_positive:that.data.userInfo[0].identify_positive,
        identify_reverse:that.data.userInfo[0].identify_reverse,
        openid:0,
        type:0
      }
    },function(res){
      mAPIUrl.save('/api/mgr/guest',{
        action: 'add',
        data:{
          room_id:that.data.roomId,
          guests_id:res.id,
          landlord_id:wx.getStorageSync('userInfo').id,
          start_rent:that.data.changeDate[0].content,
          end_rent:that.data.changeDate[1].content,
          rent:that.data.rent,
          contract:''
        }
      },function(res){
        console.log('id',res)
        mAPIUrl.update('/api/mgr/rsAddress',{
          action:'modify',
          id: that.data.roomId,
          newdata: {
            type: 1
          }
        },function(res){
          console.log(res)
        })
      })
    })
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})